package com.zhq.security.auth.handler;

import com.zhq.security.auth.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class LogOutHandler implements LogoutHandler {

    private  final  String token="Authorization";

    private static RedisTemplate redisTemplate;
    @Resource
    public void setUserRoleMapper(RedisTemplate redisTemplate) {
        LogOutHandler.redisTemplate = redisTemplate;
    }


    @Override
    public void logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) {
        String token=httpServletRequest.getHeader(this.token);
        //使用jwt解析工具解析除用户名
        if(token!=null){
            String name=new TokenUtil().parseJWT(token,"1234a");
            redisTemplate.delete("token:"+name);
        }
    }
}
